package nc.bs.hrppl.util;

import java.util.*;

public class ListMapSorter {


    public static void main(String[] args) {
        // 初始化原始列表
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> lists = new ArrayList<>();
        list.add(createMap("pk4", null, "2025-06-02 20:00:12"));
        list.add(createMap("pk1", null, "2025-06-02 20:00:00"));
        list.add(createMap("pk2", null, "2025-06-02 20:00:01"));
        list.add(createMap("pk3", 1, "2025-06-02 20:00:03"));
        list.add(createMap("pk5", null, "2025-06-02 18:00:12"));
        list.add(createMap("pk7", null, "2025-06-02 20:00:06"));
        list.add(createMap("pk8", null, "2025-06-02 20:00:08"));
        list.add(createMap("pk6", null, "2025-06-02 19:00:00"));

        List<Map<String, Object>> isNull = new ArrayList<>();
        List<Map<String, Object>> isNotNull = new ArrayList<>();
        for (Map<String, Object> m : list) {
            if (m.get("def5") == null) {
                isNull.add(m);
            }else{
                isNotNull.add(m);
            }
        }
        // 新增的map对象
        Map<String, Object> map9 = createMap("pk9", 1, "2025-06-02 20:11:12");
//        Map<String, Object> map10 = createMap("pk10", null, "2025-06-02 20:12:08");

        processList(isNotNull, map9);
        processOrderCreationTime(isNull);
        System.out.println("不为空的集合:");
        for (Map<String, Object> map : isNotNull) {
            System.out.println(map);
        }
        System.out.println("为空的集合:");
        for (Map<String, Object> map : isNull) {
            System.out.println(map);
        }
//        // 处理列表
//        processList(list, map9);
//
//
//        // 输出结果
//        System.out.println("当前列表内容:");
//        for (Map<String, Object> map : lists) {
//            System.out.println(map);
//        }

    }

    public static List<Map<String, Object>> processOrderCreationTime(List<Map<String, Object>> list){
        list.sort((m1, m2) -> {
            Object o1 = m1.get("def5");
            Object o2 = m2.get("def5");

            // 如果两个都是null，按ts排序
            if (o1 == null && o2 == null) {
                return ((String) m1.get("creationtime")).compareTo((String) m2.get("creationtime"));
            }

            // 如果其中一个为null，null的排在后面
            if (o1 == null) return 1;
            if (o2 == null) return -1;
            Integer def5_1 = Integer.valueOf(o1.toString());
            Integer def5_2 = Integer.valueOf(o2.toString());

            // 否则按def5从小到大排序
            return def5_1.compareTo(def5_2);
        });
        return list;
    }
    /**
     * 处理listMap数据
     * @param list
     * @param newMap
     * @return
     */
    public static List<Map<String, Object>> processList(List<Map<String, Object>> list, Map<String, Object> newMap) {
        // 1. 首先对列表进行排序
        list.sort((m1, m2) -> {
            Object o1 = m1.get("def5");
            Object o2 = m2.get("def5");

            // 如果两个都是null，按ts排序
            if (o1 == null && o2 == null) {
                return ((String) m1.get("creationtime")).compareTo((String) m2.get("creationtime"));
            }

            // 如果其中一个为null，null的排在后面
            if (o1 == null) return 1;
            if (o2 == null) return -1;
            Integer def5_1 = Integer.valueOf(o1.toString());
            Integer def5_2 = Integer.valueOf(o2.toString());

            // 否则按def5从小到大排序
            return def5_1.compareTo(def5_2);
        });

        // 2. 找到新数据的插入位置
        int insertIndex = 0;
//        Integer newDef5 = Integer.valueOf( newMap.get("def5").toString());
        Object newDef5 = newMap.get("def5");
        if (newDef5 != null) {
            // 如果新数据的def5不为null，找到第一个大于等于它的位置
            for (int i = 0; i < list.size(); i++) {
                Object ndef5 = list.get(i).get("def5");
//                Integer currentDef5 = Integer.valueOf(list.get(i).get("def5").toString());
//                if (currentDef5 != null && currentDef5 >= newDef5) {
                if (ndef5 != null && Integer.valueOf(list.get(i).get("def5").toString()) >= Integer.valueOf(newDef5.toString())) {
                    insertIndex = i;
                    break;
                }
                insertIndex = i + 1;
            }
        } else {
            // 如果新数据的def5为null，找到第一个null的位置
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).get("def5") == null) {
                    insertIndex = i;
                    break;
                }
            }
        }

        // 3. 如果新数据的def5与现有数据冲突，需要调整现有数据的def5
        if (newDef5 != null) {
            for (int i = 0; i < list.size(); i++) {
                Object o = list.get(i).get("def5");
//                Integer currentDef5 = Integer.valueOf( list.get(i).get("def5").toString());
                if (o != null && Integer.valueOf( o.toString()) >= Integer.valueOf(newDef5.toString())) {
                    list.get(i).put("def5", Integer.valueOf( o.toString()) + 1);
                }
            }
        }
        // 4. 插入新数据
        list.add(insertIndex, newMap);

        // 5. 重新排序
        list.sort((m1, m2) -> {
            Object o1 = m1.get("def5");
            Object o2 = m2.get("def5");

            if (o1 == null && o2 == null) {
                return ((String) m1.get("creationtime")).compareTo((String) m2.get("creationtime"));
            }

            if (o1 == null) return 1;
            if (o2 == null) return -1;

            Integer def5_1 = Integer.valueOf( o1.toString());
            Integer def5_2 = Integer.valueOf( o2.toString());

            return def5_1.compareTo(def5_2);
        });

        // 6. 重新分配def5值，使其从1开始递增
        int currentDef5 = 1;
        for (Map<String, Object> map : list) {
            if (map.get("def5") != null) {
                map.put("def5", currentDef5++);
            }

        }

        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            if (map.get("def5") == null){
                map.put("def5",i+1);
            }
        }

        return list;
    }

    private static Map<String, Object> createMap(String pk1, Integer def5, String ts) {
        Map<String, Object> map = new HashMap<>();
        map.put("pk1", pk1);
        map.put("def5", def5);
        map.put("creationtime", ts);
        return map;
    }

}
